package org.example.myleet.p1413;

public class Solution {
    public int minStartValue(int[] nums) {
        int res = nums[0] > 0 ? 1 : (Math.abs(nums[0]) + 1);
        int minSum = nums[0];
        int preSum = nums[0];
        for (int i = 1; i < nums.length; ++i) {
            int sum = preSum + nums[i];
            if (sum < minSum) {
                minSum = sum;
                res = sum > 0 ? 1 : Math.abs(sum) + 1;
            }
            preSum = sum;
        }
        return res;
    }
}
